package com.mike.employment.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mike.employment.base.BaseDao;
import com.mike.employment.base.BaseServiceImpl;
import com.mike.employment.base.exception.BusinessExcep;
import com.mike.employment.dao.AccountDao;
import com.mike.employment.dao.CompanyDao;
import com.mike.employment.po.Account;
import com.mike.employment.po.Company;
import com.mike.employment.service.CompanyService;
import com.mike.employment.vo.CompanyVo;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

@Service
public class CompanyServiceImpl extends BaseServiceImpl<Company> implements CompanyService{
	
	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private AccountDao accountDao;
	@PersistenceContext
	private EntityManager entityManager;

	@Override
	public BaseDao<Company> getRepository() {
		return companyDao;
	}

	@Override
	public EntityManager getEntity() {
		return entityManager;
	}

	@Override
	public Account updateInfo(CompanyVo vo) {
		Company company = companyDao.saveAndFlush(vo.getCompany());
		Account account = accountDao.findById(vo.getAccount()).get();
		if(account != null) {
			account.setCompany(company);
			return accountDao.saveAndFlush(account);
		}else {
			throw new BusinessExcep("账号不存在");
		}
	}

}
